<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DictController extends Controller
{
    /**
     * 获取字典数据映射
     * 返回所有字典类型的数据映射
     */
    public function getDictData(Request $request)
    {
        // 模拟字典数据，实际项目中应该从数据库获取
        $dictData = [
            'status' => [
                ['label' => '启用', 'value' => '1'],
                ['label' => '禁用', 'value' => '0'],
            ],
            'gender' => [
                ['label' => '男', 'value' => 'male'],
                ['label' => '女', 'value' => 'female'],
            ],
            'user_type' => [
                ['label' => '管理员', 'value' => 'admin'],
                ['label' => '普通用户', 'value' => 'user'],
            ],
        ];

        return response()->json([
            'data' => $dictData,
            'message' => '获取成功'
        ]);
    }

    /**
     * 获取字典数据列表（分页）
     */
    public function getDictDataList(Request $request)
    {
        $code = $request->input('code', '');
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);

        // 模拟分页数据
        $data = [
            ['id' => 1, 'label' => '启用', 'value' => '1', 'code' => 'status'],
            ['id' => 2, 'label' => '禁用', 'value' => '0', 'code' => 'status'],
        ];

        if ($code) {
            $data = array_filter($data, function($item) use ($code) {
                return $item['code'] === $code;
            });
        }

        return response()->json([
            'data' => [
                'list' => array_values($data),
                'total' => count($data),
                'page' => $page,
                'size' => $size,
            ],
            'message' => '获取成功'
        ]);
    }
}
